package com.fxust.thread;

/**
 * @auto johnking
 * @create 2017-12-17 下午9:22
 **/

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import com.fxust.common.CommonDefine;
import com.fxust.common.Constant;
import com.fxust.dao.ThirdTableDao;
import com.fxust.dialog.BaseDialog;
import org.apache.log4j.Logger;

public class DownLoadPicThread extends Thread {

    private static Logger logger = Logger.getLogger(DownLoadPicThread.class);

    private String uuid;
    private Map<String, String> param;

    public DownLoadPicThread(String uuid, Map<String, String> param) {
        this.uuid = uuid;
        this.param = param;
    }

    @Override
    public void run() {

        // 线程池队列、等待队列都固定为3
        int threadCount = 3;
        // 线程存活时间(天)
        int keepAliveTime = 2;

        // 线程池执行策略
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(threadCount);
        RejectedExecutionHandler executionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, keepAliveTime, TimeUnit.DAYS,
                queue, executionHandler);

        BaseDialog dialog = CommonDefine.asyncTrainMap.get(uuid);
        int totalCount = 0;
        int pageBegin = 0;
        String filePath = param.get(Constant.FILE_PATH);
        String inputFlag = param.get(Constant.INPUT_FLAG);

        //若用户无输入,则按照数据库总量进行处理
        if (Constant.NO.equals(inputFlag)) {
            totalCount = queryDataCount();
            if (totalCount == 0) {
                logger.error("Error:查询数据量为零,请检查sql语句");
                JOptionPane.showMessageDialog(null, "查询数据量为零", "错误信息", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } else {//若用户有输入,按照输入数据进行分页处理
            totalCount = Integer.valueOf(param.get(Constant.TOTAL_NUM));
            pageBegin = Integer.valueOf(param.get(Constant.START_NUM));
        }
        // 分页处理
        try {
            if (totalCount > 0) {
                short pageSize = 1000;
                int pageNum = totalCount / pageSize;
                int surplus = totalCount % pageSize;
                if (surplus > 0) {
                    pageNum = pageNum + 1;
                }
                for (int cp = 0; cp < pageNum; cp++) {
                    int pageStart = pageSize * cp + pageBegin;
                    int pageEnd = pageStart + pageSize;
                    executor.execute(new DownLoadPicDetailThread(filePath, cp, pageStart, pageEnd));
                    int statisics = (surplus > 0) ? (surplus + cp * pageSize) : (surplus + (cp + 1) * pageSize);
                    dialog.setDealNumFieldValue("下载数量:" + statisics);
                    dialog.setDealProgressBarValue(statisics * 100 / totalCount);
                }

            } else {
                logger.error("获取数据总量出错,人员总数为0");
            }
        } catch (Exception e) {
            logger.error("下载图片异常", e);
        }
    }

    /**
     * 获取数据总量
     * @author fgq 2017年12月5日 下午4:57:41
     * @return
     */
    private int queryDataCount() {
        ThirdTableDao dao = new ThirdTableDao();
        int count = dao.queryTableDataCount(CommonDefine.queryCountSql);
        return count;
    }
}
